package com.shuoyue.ycgk.ui.mine.wrong;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.ChapterType;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.SectionType;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.mine.wrong.infos2.QuestionWrongActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;
import com.shuoyue.ycgk.views.dialog.common.HintDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentWrongTab extends BaseMvpFragment<WrongContract.Presenter>
        implements WrongContract.View {
    Type type;
    String title;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    @BindView(R.id.clear)
    TextView clear;
    @BindView(R.id.delete)
    TextView delete;

    ChapterAdapter wrongAdapter;

    @Override
    public void initParams(Bundle bundle) {
        type = (Type) bundle.getSerializable("type");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wrong;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getWrongTypes(type.getId());
            refreshLayout.finishRefresh();
        });
        wrongAdapter = new ChapterAdapter(null);
        wrongAdapter.setDoquestions(new ChapterAdapter.Doquestions() {
            @Override
            public void done(int sectionId, String name) {
                QuestionWrongActivity.start(mActivity, sectionId, 1, name);
            }

            @Override
            public void redone(int sectionId, String name) {
                QuestionWrongActivity.start(mActivity, sectionId, 0, name);
            }

            @Override
            public void delete(ChapterType chapterType, SectionType sectionType) {
                mPresenter.deleteWrongType(chapterType, sectionType);
            }
        });
        wrongAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ChapterType specialType = (ChapterType) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_title:
                    specialType.setSelect(!specialType.isSelect());
                    adapter.notifyDataSetChanged();
                    break;
                case R.id.delete:
                    HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                            "提示",
                            " 是否确认删除?");
                    hintConfirmDialog.setOkListener(v -> {
                        mPresenter.deleteWrongType(specialType, null);
                        hintConfirmDialog.dismiss();
                    });
                    hintConfirmDialog.show();
                    break;
            }
        });
        recyclerView.setAdapter(wrongAdapter);
        registEmptyView(wrongAdapter);
    }

    @Override
    protected void initData() {
        mPresenter = new WrongContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getWrongTypes(type.getId());
    }


    public static FragmentWrongTab getInstance(Type type) {
        FragmentWrongTab fragment = new FragmentWrongTab();
        fragment.title = type.getName();
        Bundle bundle = new Bundle();
        bundle.putSerializable("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void setTypeList(List<ChapterType> chapterSectionTypes) {
        wrongAdapter.resetData(chapterSectionTypes);

        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getWrongTypes(type.getId());
    }

    @Override
    public void setQuestions(List<QuestionParent> questionList) {

    }

    @Override
    public void clearSuc() {
//        wrongAdapter.clear();
        mPresenter.getWrongTypes(type.getId());

    }

    @Override
    public void deleted(ChapterType chapter, SectionType sectionType) {
//        if (chapter != null) {
//            wrongAdapter.getData().remove(chapter);
//        }
//        if(sectionType!=null){
//            chapter.getWrongSectionDTOS()
//        }
        mPresenter.getWrongTypes(type.getId());
    }


    @Override
    public void changeStatue(int statue) {

    }

    @Override
    public String getTitle() {
        return title;
    }

    @OnClick({R.id.clear, R.id.delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear:
                HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                        "提示",
                        "准备清空错题本,是否确认?");
                hintConfirmDialog.setOkListener(v -> {
                    mPresenter.deleteAll(type.getId());
                    hintConfirmDialog.dismiss();
                });
                hintConfirmDialog.show();
                break;
            case R.id.delete:
                wrongAdapter.setDelete(!wrongAdapter.isDelete());
                wrongAdapter.notifyDataSetChanged();
                break;
        }
    }
}
